﻿#include "emqxcontroller.h"
#include <QDir>
#include <QTimer>
#include <QDebug>

EmqxController::EmqxController(QObject *parent)
    : QObject(parent), process(new QProcess(this)), currentStatus(Unknown)
{
    emqxPath = "D:/emqx-5.3.2-windows-amd64"; // 可改为配置文件读取
}

EmqxController::~EmqxController()
{
    if (process->state() == QProcess::Running) {
        process->terminate();
        process->waitForFinished(3000);
    }
}

void EmqxController::runCommand(const QStringList &args)
{
    QString cmd = emqxPath + "/bin/emqx.cmd";
    qDebug() << "Executing:" << cmd << args;

    process->start(cmd, args);
    if (!process->waitForStarted()) {
        emit errorReceived("Failed to start emqx.cmd: " + process->errorString());
        return;
    }

    if (process->waitForFinished(10000)) {
        QString out = QString::fromLocal8Bit(process->readAllStandardOutput());
        QString err = QString::fromLocal8Bit(process->readAllStandardError());
        if(args.contains("ping")){
            stateoutstrings="";
            stateoutstrings=out+err;
        }
        if(args.contains("start")){
            startBack="";
            startBack=out+err;
        }
        if(args.contains("restart")){
            restartBack="";
            restartBack=out+err;
        }
        emit outputReceived(out);
        if (!err.isEmpty()) {
            emit errorReceived(err);
        }
    } else {
        process->kill();
        emit errorReceived("Command timeout.");
    }
}

EmqxController::Status EmqxController::getStatus()
{
    runCommand({"ping"});

    if (stateoutstrings.contains("pong")) {
        currentStatus = Running;
    } else{
        currentStatus = Stopped;
    }
    emit statusChanged(currentStatus);
    return currentStatus;
}

bool EmqxController::start()
{
    runCommand({"start"});
    if (startBack.contains("core") || startBack.contains("mnesia")) {
        currentStatus = Running;
        emit statusChanged(currentStatus);
        return true;
    }
    return false;
}

bool EmqxController::stop()
{
    runCommand({"stop"});

    runCommand({"ping"});

    if(stateoutstrings.contains("not")) {
        currentStatus = Stopped;
        emit statusChanged(currentStatus);
        return  true;
    }
    else {
        return  false;
    }

}

bool EmqxController::restart()
{
    stop();
    QEventLoop loop;
    QTimer::singleShot(2000, &loop, &QEventLoop::quit);
    loop.exec(); // 等待2秒再启动
    return start();
}

bool EmqxController::install()
{
    runCommand({"install"});
    QString out = QString::fromLocal8Bit(process->readAllStandardOutput()).toLower();
    return out.contains("installed") || out.contains("already installed");
}

bool EmqxController::uninstall()
{
    stop(); // 先停止
    runCommand({"uninstall"});
    QString out = QString::fromLocal8Bit(process->readAllStandardOutput()).toLower();
    return out.contains("uninstalled") || out.contains("not installed");
}

QString EmqxController::getOutput() const
{
    return QString::fromLocal8Bit(process->readAllStandardOutput());
}

QString EmqxController::getError() const
{
    return QString::fromLocal8Bit(process->readAllStandardError());
}
